package com.point_of_sale.tests;

import static org.junit.Assert.*;

import java.math.BigDecimal;

import org.junit.Test;

import com.point_of_sale.terminal.NoProductListLoaderException;
import com.point_of_sale.terminal.Terminal;

public class TerminalTest 
{
	@Test
	public void testTerminalOne() 
	{
		BigDecimal expected = new BigDecimal(32.4);
		expected = expected.setScale(2, BigDecimal.ROUND_HALF_EVEN);
		
		Terminal terminal = new Terminal();
		try {
			terminal.loadProducts("bin/com/point_of_sale/assets/product_list.txt");
		} catch (NoProductListLoaderException e) {
			
			System.err.println(e.getMessage());
		}
		
		terminal.scan("A");
		terminal.scan("B");
		terminal.scan("C");
		terminal.scan("D");
		terminal.scan("A");
		terminal.scan("B");
		terminal.scan("A");
		terminal.scan("A");
		
		BigDecimal actual = terminal.checkOut();
		
		assertEquals(expected, actual);
	}
	
	@Test
	public void testTerminalTwo() 
	{
		BigDecimal expected = new BigDecimal(7.25);
		expected = expected.setScale(2, BigDecimal.ROUND_HALF_EVEN);
		
		Terminal terminal = new Terminal();
		try {
			terminal.loadProducts("bin/com/point_of_sale/assets/product_list.txt");
		} catch (NoProductListLoaderException e) {
			
			System.err.println(e.getMessage());
		}
		
		terminal.scan("C");
		terminal.scan("C");
		terminal.scan("C");
		terminal.scan("C");
		terminal.scan("C");
		terminal.scan("C");
		terminal.scan("C");
		
		BigDecimal actual = terminal.checkOut();
		
		assertEquals(expected, actual);
	}
	
	@Test
	public void testTerminalThree() 
	{
		BigDecimal expected = new BigDecimal(15.40);
		expected = expected.setScale(2, BigDecimal.ROUND_HALF_EVEN);
		
		Terminal terminal = new Terminal();
		try {
			terminal.loadProducts("bin/com/point_of_sale/assets/product_list.txt");
		} catch (NoProductListLoaderException e) {
			
			System.err.println(e.getMessage());
		}
		
		terminal.scan("A");
		terminal.scan("B");
		terminal.scan("C");
		terminal.scan("D");
		
		BigDecimal actual = terminal.checkOut();
		
		assertEquals(expected, actual);
	}
	
	
	// Added test for multiple combinations (i.e. 2 six packs)
	@Test
	public void testTerminalFour() 
	{
		BigDecimal expected = new BigDecimal(12.00);
		expected = expected.setScale(2, BigDecimal.ROUND_HALF_EVEN);
		
		Terminal terminal = new Terminal();
		try {
			terminal.loadProducts("bin/com/point_of_sale/assets/product_list.txt");
		} catch (NoProductListLoaderException e) {
			
			System.err.println(e.getMessage());
		}
		
		terminal.scan("C");
		terminal.scan("C");
		terminal.scan("C");
		terminal.scan("C");
		terminal.scan("C");
		terminal.scan("C");
		terminal.scan("C");
		terminal.scan("C");
		terminal.scan("C");
		terminal.scan("C");
		terminal.scan("C");
		terminal.scan("C");
		
		BigDecimal actual = terminal.checkOut();
		
		assertEquals(expected, actual);
	}
	
	// Making sure that the checkout method clears the cart
	@Test
	public void testTerminalFive() 
	{
		BigDecimal expected = new BigDecimal(0.15);
		expected = expected.setScale(2, BigDecimal.ROUND_HALF_EVEN);
		
		Terminal terminal = new Terminal();
		try {
			terminal.loadProducts("bin/com/point_of_sale/assets/product_list.txt");
		} catch (NoProductListLoaderException e) {
			
			System.err.println(e.getMessage());
		}
		
		terminal.scan("C");
		terminal.scan("C");
		
		BigDecimal actual = terminal.checkOut();
		
		terminal.scan("D");
		
		actual = terminal.checkOut();
		
		assertEquals(expected, actual);
	}
}
